package 系统复习.面试题.滑动窗口;

import java.util.ArrayList;
import java.util.List;

/**
 * 给你一个字符串 S，找出所有长度为 K 且不含重复字符的子串，请你返回全部满足要求的子串的 数目。
 */
public class L1100 {

    public int numKLenSubstrNoRepeats(String s, int k) {

        int count = 0;
        List<Character> list = new ArrayList<>();
        for (int l = 0, r = 0; r < s.length(); r++) {
            char c = s.charAt(r);
            if (list.contains(c)) {
                while (s.charAt(l) != c) {
                    l++;
                    list.remove(0);
                }
                l++;
                list.remove(0);
                list.add(c);
            } else {
                list.add(c);
                if (list.size() == k) {
                    count++;
                    l++;
                    list.remove(0);
                }
            }
        }
        return count;
    }

    public static void main(String[] args) {
        L1100 l1100 = new L1100();
        System.out.println(l1100.numKLenSubstrNoRepeats("abab", 2));
    }
}
